<?php

class Contract extends Model
{

    public $sub_total = 0;
    public $vat_total = 0;
    public $grand_total = 0;
    public $viewUrl;

    public function tableName()
    {
        return 'contract';
    }

    public function rules()
    {
        return array(
            array(
                'name, tax, address, date, contract',
                'required', 'on' => 'insert'
            ),
            array(
                'products, niemphong, status',
                'numerical',
                'integerOnly' => true
            ),
            array(
                'name, tax, director, position, date, contract, datelog, userlog, datebb, tamung, ngaygiao, marketing',
                'length',
                'max' => 255
            ),
            array(
                'contract_id, name, tax, address, director, position, date, contract, datelog, userlog, datebb, products, niemphong, tamung, ngaygiao, marketing, note, status',
                'safe',
            ),
            array(
                'contract_id, name, tax, address, director, position, date, contract, datelog, userlog, datebb, products, niemphong, tamung, ngaygiao, marketing, note, status',
                'safe',
                'on' => 'search'
            )
        );
    }

    public function relations()
    {
        return array(
            'product' => array(
                self::HAS_MANY,
                'Product',
                'contract_id'
            ),
        );
    }

    public function attributeLabels()
    {
        return array(
            'contract_id' => 'STT',
            'name' => 'Công ty',
            'tax' => 'MST',
            'address' => 'Địa chỉ',
            'director' => 'Người đại diện',
            'position' => 'Chức vụ',
            'date' => 'Ngày HĐ',
            'contract' => 'Số HĐ',
            'datelog' => 'Ngày cập nhật',
            'userlog' => 'Người cập nhật',
            'datebb' => 'Ngày biên bản',
            'products' => 'Số sản phẩm',
            'niemphong' => 'Niêm phong',
            'tamung' => 'Tạm ứng',
            'ngaygiao' => 'Ngày giao',
            'marketing' => 'Nhân viên KD',
            'note' => 'Ghi chú',
            'status' => 'Trạng thái'
        );
    }

    public function search()
    {
        $criteria = new CDbCriteria;
        $criteria->compare('contract_id', $this->contract_id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('tax', $this->tax, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('director', $this->director, true);
        $criteria->compare('position', $this->position, true);
        $criteria->compare('date', $this->date, true);
        $criteria->compare('contract', $this->contract, true);
        $criteria->compare('datelog', $this->datelog, true);
        $criteria->compare('userlog', $this->userlog, true);
        $criteria->compare('datebb', $this->datebb, true);
        $criteria->compare('products', $this->products);
        $criteria->compare('niemphong', $this->niemphong);
        $criteria->compare('tamung', $this->tamung, true);
        $criteria->compare('ngaygiao', $this->ngaygiao, true);
        $criteria->compare('marketing', $this->marketing, true);
        $criteria->compare('note', $this->note, true);
        $criteria->compare('status', $this->status);
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'sort' => array(
                'defaultOrder' => 'date DESC',
            )
        ));
    }

    protected function afterFind()
    {
        parent::afterFind();
        if ($this['product']) {
            foreach ($this['product'] as $product) {
                $this->sub_total += $product->price * $product->quantity;
            }
            $this->vat_total = round($this->sub_total * 10 / 100);
            $this->grand_total = $this->vat_total + $this->sub_total;
        }
        $this->viewUrl = Yii::app()->createUrl('contract/view', array('id' => $this->contract_id));
    }

    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    protected function beforeSave()
    {
        $this->date = strtotime(implode("/", array_reverse(explode("-", $this->date))));
        return parent::beforeSave();
    }
}